﻿using NpcFramework;
using System.Collections.Generic;
using UnityEngine;


namespace com.yoozoo.gta.Gameplay.PVE
{
    public class SceneSeparateManager : BaseSceneSeparateManager
    {
        public enum SceneSeparateType
        {
            None,
            LibertyScene,
        }

        public SceneSeparateType separateType = SceneSeparateType.None;
        
        //场景对象加载控制器
        private SceneObjectLoadController m_Controller;

        public NpcGroupData groupData;
        private Dictionary<int, NpcBrithPointData> allPointDataDic;

        public void InitData(SceneSeparateManagerData ssmd)
        {
            sceneData = ssmd;
            if (sceneData.allPointData!=null)
            {
                allPointDataDic = new Dictionary<int, NpcBrithPointData>(sceneData.allPointData.Count);
                foreach (var pointData in sceneData.allPointData)
                {
                    allPointDataDic.Add(pointData.Index,pointData);
                }
            }
        }

        public void SetLoadObjectActive(bool active)
        {
            if (!active)
            {
                m_Controller.DestroyBirthPointObj();
            }
            foreach (var staticObj in sceneData.npcPoints)
            {
                if (active)
                {
                    //staticObj.OnShow(transform);
                }
                else
                {
                    staticObj.OnHide();
                }
            }
        }
        public void Dispose()
        {
            allPointDataDic.Clear();

            foreach (var npcPoints in sceneData.npcPoints)
            {
                npcPoints.OnHide();
            }
            //foreach (var staticObj in sceneData.staticObjects)
            //{
            //    staticObj.OnHide();
            //}
            //foreach (var loadObject in sceneData.loadObjects)
            //{
            //    loadObject.OnHide();
            //}
            foreach (var staticObj in sceneData._staticObjects)
            {
                staticObj.OnHide();
            }
            foreach (var loadObject in sceneData._loadObjects)
            {
                loadObject.OnHide();
            }

            m_Controller?.DestroyBirthPointObj();
            Destroy(gameObject);
        }

        
        public NpcBrithPointData GetNpcPointData(int index)
        {
            return allPointDataDic[index];
        }

        void Start()
        {
            //初始化
            m_Controller = gameObject.GetComponent<SceneObjectLoadController>();
            if (m_Controller == null)
                m_Controller = gameObject.AddComponent<SceneObjectLoadController>();
            m_Controller.Init(sceneData.bounds.center, sceneData.bounds.size, sceneData.asyn, sceneData.treeType);


            for (int i = 0; i < sceneData.npcPoints.Count; i++)
            {
                m_Controller.AddSceneBlockObject(sceneData.npcPoints[i]);
            }

            // TODO LibertyScene场景暂时不考虑动态加载场景，美术还没规范，并且和Lua业务有耦合
            if (separateType != SceneSeparateType.LibertyScene)
            {
                //添加场景对象
                //for (int i = 0; i < sceneData.loadObjects.Count; i++)
                //{
                //    m_Controller.AddSceneBlockObject(sceneData.loadObjects[i]);
                //}

                foreach (var loadObject in sceneData._loadObjects)
                {
                    m_Controller.AddSceneBlockObject(loadObject);
                }

                GenerateStaticObjs();
            }

        }

        public NpcGroupInfo GetNpcGroupInfo(int id)
        {
            if (groupData == null)
            {
                return null;
            }
            foreach (var info in groupData.GroupInfoList)
            {
                if (info.GroupId == id)
                {
                    return info;
                }
            }

            return null;
        }

        private void GenerateStaticObjs()
        {
            GameObject staticObjs = new GameObject("StaticObjs");
            staticObjs.transform.SetParent(this.transform);
            var pt = staticObjs.transform;
            //for (int i = 0; i < sceneData.staticObjects.Count; i++)
            //{
            //    var sceneObjData = sceneData.staticObjects[i];
            //    sceneObjData.OnShow(pt);
            //}
            foreach (var staticObject in sceneData._staticObjects)
            {
                staticObject.OnShow(pt);
            }
        }

        void OnGUI()
        {
            GUI.color = Color.red;
            if (sceneData)
            {
                GUILayout.Label(sceneData.desc);
            }

        }

        void Update()
        {

            if (NFMgr.isPause)
            {
                return;
            }
            
            if (sceneData.detector)
            {
                //每帧处理
                m_Controller.RefreshDetector(sceneData.detector);
            }

        }
  
        private void OnDestroy()
        {
            if (sceneData)
            {
                //for (int i = 0; i < sceneData.staticObjects.Count; i++)
                //{
                //    var sceneObjData = sceneData.staticObjects[i];
                //    sceneObjData.OnHide();
                //}

                foreach (var staticObject in sceneData._staticObjects)
                {
                    staticObject.OnHide();
                }

                sceneData = null;
            }


        }

        // public void InitNpcFramework(NpcFrameworkMgr frameworkMgr)
        // {
        //     if (sceneData)
        //     {
        //         foreach (var wayPath in sceneData.wayPaths)
        //         {
        //             frameworkMgr.AddNpcWayPath(wayPath);
        //         }
        //
        //         frameworkMgr.InitCarBirthPoint();
        //     }
        //
        // }

        void OnDrawGizmosSelected()
        {
            if (Application.isPlaying)
            {
            }
            else
            {
                if (sceneData)
                {
                    sceneData.bounds.DrawBounds(Color.green);
                }

            }
        }

       
    }

}
